//
//  GoodShopDetailViewController.m
//  shise
//
//  Created by 徐国庆 on 14/12/29.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "GoodShopDetailViewController.h"
#import "NetHandler.h"
#import "Food.h"
#import "MenuCell.h"
#import "UIImageView+WebCache.h"
#import "ViewController.h"
#import "DetailViewController.h"


@interface GoodShopDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *array;

@end

@implementation GoodShopDetailViewController

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self handleData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Food *food = [self.array objectAtIndex:indexPath.row];
    if ([food.type isEqualToString:@"PicCard"]) {
        
        NSString *string = @"goodShopDetail";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
        if (cell == nil) {
            
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string]autorelease];
        }
//        NSString *str = [NSString stringWithFormat:@"%@\n%@", food.userName, food.desc];
        cell.textLabel.text = food.userName;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.alpha = 0.8;
        cell.detailTextLabel.text = food.desc;
        cell.detailTextLabel.numberOfLines = 0;
        cell.detailTextLabel.alpha = 0.5;
        return cell;
    } else if ([food.type isEqualToString:@"TitleCard"]){
        NSString *string = @"goodShopTitleCard";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
        if (cell == nil) {
            
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
        }
        cell.textLabel.text = food.userName;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.alpha = 0.8;
        return cell;
    } else if ([food.type isEqualToString:@"TwoCard"]) {
        
        NSString *string = @"goodShopTwoCards";
        MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
        if (cell == nil) {
            
            cell = [[[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
        }
        
        cell.textLabel.alpha = 0.5;
        [cell.menuOne setImageWithURL:[NSURL URLWithString:food.menuOne]];
        [cell.menuTwo setImageWithURL:[NSURL URLWithString:food.menuTwo]];
        // 为按钮设置tag值 当点击cell的时候通过tag取出数组中的数据
        cell.menuOneButton.tag = 2000 + indexPath.row;
        [cell.menuOneButton addTarget:self action:@selector(menuButtonOneAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.menuTwoButton.tag = 3000 + indexPath.row;
        [cell.menuTwoButton addTarget:self action:@selector(menuButtonTwoAction:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    } else {
        
        NSString *string = @"nihao";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
        if (cell == nil) {
            
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];
        }
        return cell;
    }
}

// 设置点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Food *food = [self.array objectAtIndex:indexPath.row];
    if ([food.type isEqualToString:@"TitleCard"]) {
        
        ViewController *VC = [[ViewController alloc] init];
        VC.str = food.url;
        [self.navigationController pushViewController:VC animated:YES];
        [VC release];
    }
}

// 点击按钮的方法
- (void)menuButtonOneAction:(UIButton *)button
{
//    NSLog(@"点击第一个按钮");
    Food *food = [self.array objectAtIndex:button.tag - 2000];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.detailApi = food.detailApiOne;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
//    ViewController *VC = [[ViewController alloc] init];
//    VC.str = food.webViewUrlOne;
//    [self.navigationController pushViewController:VC animated:YES];
//    [VC release];
}

// 点击第二个按钮的方法
- (void)menuButtonTwoAction:(UIButton *)button
{
//    NSLog(@"点击了第二个按钮");
    Food *food = [self.array objectAtIndex:button.tag - 3000];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.detailApi = food.detailApiTwo;
    if (food.detailApiTwo.length > 0) {
        
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    [detailVC release];
//    ViewController *VC = [[ViewController alloc] init];
//    VC.str = food.webViewUrlTwo;
//    [self.navigationController pushViewController:VC animated:YES];
//    [VC release];
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Food *food = [self.array objectAtIndex:indexPath.row];
    if ([food.type isEqualToString:@"PicCard"]) {
        
        return 80;
    } else if ([food.type isEqualToString:@"TitleCard"]) {
        
        return 44;
    } else if ([food.type isEqualToString:@"TwoCard"]) {
        
        return 170;
    } else {
        
        return 0;
    }
}


// 自定义处理数据的方法
- (void)handleData
{
//    NSString *string = @"https://api.shiseapp.com/v2/shop/p_oMNNVgmGC.ChKAMW/?_city=重庆&_device=Matcha%20v2.5.0%20%28iPhone%204S,%207.1%29&_version=2.5.0&conf_ver=1419737046&gps=38.882875,121.539560&uuid=44cd1914a02fab9e63fb7ec1fbf910e0";
    NSString *string = [NSString stringWithFormat:@"https://api.shiseapp.com%@", self.address];
    [NetHandler getDataWithUrl:string completion:^(NSData *data) {
        
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error: &error];
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *tempDic in arr) {
            
            Food *food = [[Food alloc] initWithDictionary:tempDic];
            [self.array addObject:food];
            [food release];
        }
        [self.tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
